package com.lp.service.impl;

import com.lp.bo.*;
import com.lp.common.CodeIot;
import com.lp.service.IotOtherInfoService;
import com.lp.util.ObjectUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IotOtherInfoServiceImpl extends BaseServiceImpl implements IotOtherInfoService {

	@Override
	public Map<String, Object> getAppStatusInfo(UserInfoBO user) {
		Map<String,Object> resultMap = getResultMap();
		try{
			// 场景数据
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id( user.getId() );
			Integer num = dao.selectCount("IotSceneInfo.selectCount", obj);
			if(ObjectUtil.isEmpty(num)){
				num = 0;
			}
			resultMap.put("sceneNum", num);
			// 传感器数
			IotSensorInfoBO iotSensorInfo = new IotSensorInfoBO();
			iotSensorInfo.setUser_id(user.getId());
			Integer sensorNum = dao.selectCount("IotSensorInfo.selectPageCount", iotSensorInfo);
			if(ObjectUtil.isEmpty(sensorNum)){
				sensorNum = 0;
			}
			resultMap.put("sensorNum", sensorNum);
			// 触发器数
			IotTriggerInfoBO triggerInfo = new IotTriggerInfoBO();
			triggerInfo.setUser_id(user.getId());
			Integer triggerNum = dao.selectCount("IotTriggerInfo.selectPageCount", triggerInfo);
			if(ObjectUtil.isEmpty(triggerNum)){
				triggerNum = 0;
			}
			resultMap.put("triggerNum", triggerNum);
			// 子账户数
			UserInfoBO userInfo = new UserInfoBO();
			userInfo.setAid(user.getId());
			Integer subUserNum = dao.selectCount("UserInfo.selectCount"	, userInfo);
			if(ObjectUtil.isEmpty(subUserNum)){
				subUserNum = 0;
			}
			resultMap.put("subUserNum", subUserNum);
			// 设备总数 设备离线数
			IotNodeInfoBO nodeInfo = new IotNodeInfoBO();
			nodeInfo.setUser_id(user.getId());
			List<CommonInfoBO> nodeNum = dao.selectList("IotNodeInfo.selectNodeNumGroup", nodeInfo);
			int onlineNum =0,offlineNum=0 ;
			for(CommonInfoBO in : nodeNum){
				if( in.getParam() == CodeIot.DEVICE_STATUS.ONLINE){
					onlineNum+=in.getNum();
				}else{
					offlineNum += in.getNum() ;
				}
			}
			resultMap.put("onlineNum", onlineNum);
			resultMap.put("offlineNum", offlineNum);
			//今日报警数和已处理数量
			IotAlarmInfoBO alarmInfo = new IotAlarmInfoBO();
			alarmInfo.setUser_id(user.getId());
			alarmInfo.setAtime(new Date());
			List<CommonInfoBO> alarmNum =dao.selectList("IotAlarmInfo.selectAlarmNumGroup", alarmInfo);
			int solveNum =0 , unsolveNum = 0;
			for(CommonInfoBO in : alarmNum){
				if( in.getParam() == CodeIot.PROCESS_STATUS.NO){
					unsolveNum+=in.getNum();
				}else{
					solveNum += in.getNum() ;
				}
			}
			resultMap.put("solveNum", solveNum);
			resultMap.put("unsolveNum", unsolveNum);
			alarmInfo.setAtime(null);
			List<CommonInfoBO> alarmNumHistory =dao.selectList("IotAlarmInfo.selectAlarmNumGroup", alarmInfo);
			int solveNumHistory =0 , unsolveNumHistory = 0;
			for(CommonInfoBO in : alarmNumHistory){
				if( in.getParam() == CodeIot.PROCESS_STATUS.NO){
					unsolveNumHistory +=in.getNum();
				}else{
					solveNumHistory += in.getNum() ;
				}
			}
			resultMap.put("solveNumHistory", solveNumHistory);
			resultMap.put("unsolveNumHistory", unsolveNumHistory);
		}catch (Exception e) {
			super.exception(e, resultMap, user);
		}
		return resultMap;
	}
}
